/*
 * @Description: 标准性检测Reducer
 * @Author: Rfan
 * @Date: 2022-10-09 11:36:40
 * @LastEditTime: 2022-10-21 18:12:41
 */

import {
  ADD_DETECTION_PLAN,
  DELETE_DETECTION_PLAN,
  EDIT_DETECTION_PLAN,
  GET_DETECTION_PLAN_LIST,
  SET_LEVEL_LIST,
  SET_SELECT_AT,
  SET_SELECT_FOND
} from '../constants';
import { delItemByIdArray, editItemById, insertItem } from '@/utils/list';
import { SET_SELECTED_ITEM } from '@/view/system/Units/constants';

export const ArchivesDetectionReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 表格加载loading
     */
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };

    // 表格选中的项目
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_LEVEL_LIST:
      return {
        ...state,
        levelList: action.data,
        levelListWithoutERecord: action.data.filter((level) => level.level !== 0)
      };

    /**
     * @description: 获取检测方案列表
     */
    case GET_DETECTION_PLAN_LIST:
      return {
        ...state,
        list: action.data
      };

    /**
     * @description: 添加检测方案
     */
    case ADD_DETECTION_PLAN: {
      return {
        ...state,
        list: insertItem(action.data, state.list)
      };
    }

    /**
     * @description: 编辑检测方案
     */
    case EDIT_DETECTION_PLAN: {
      const { id, data } = action;
      return {
        ...state,
        list: editItemById(id, data, state.list)
      };
    }

    /**
     * @description: 删除检测方案
     */
    case DELETE_DETECTION_PLAN: {
      return {
        ...state,
        list: delItemByIdArray(action.ids, state.list)
      };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
